import Notify from '../../vant/notify/notify';
//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
    data: {
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        isAuthor: false,
        showLoginBox: false,
        loading: false,
        username: '',
        password: ''
    },
    bindUserName(e) {
        this.setData({
            username: e.detail
        })
    },
    bindPwd(e) {
        this.setData({
            password: e.detail
        })
    },
    goRegist() {
        let url = '/pages/register/register'
        wx.navigateTo({
            url
        });
    },
    getUserInfo(e) {
        let userInfo = e.detail.userInfo
        app.globalData.userInfo = userInfo
        this.checkAuthor(userInfo);
    },
    handleClick() {
        let _this = this;
        if (_this.data.username == '') {
            Notify({
                text: '请输入用户名',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }
        if (_this.data.password == '') {
            Notify({
                text: '请输入密码',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }

        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '登录中...'
        })
        app.ajax({
            url: `${config.httpUrl}log/doLoginWxApp`,
            method: 'post',
            data: {
                username: _this.data.username,
                password: _this.data.password
            },
            callback: function(error, data) {
                if (+data.data.code === 0) {
                    _this.setData({
                        loading: false
                    })
                    let url = '/pages/index/index'
                    wx.reLaunch({
                        url
                    });
                } else {
                    Notify({
                        text: data.data.msg,
                        duration: 1000,
                        selector: '#custom-selector'
                    });
                }
                wx.hideLoading();
                _this.setData({
                    loading: false
                })
            }
        })
    },
    checkAuthor(userInfo) {
        let _this = this;
        if (!!userInfo) {
            wx.showLoading({
                title: '授权中...'
            })
            app.ajax({
                url: `${config.httpUrl}log/binding`,
                method: 'get',
                data: {},
                callback: function(error, data) {
                    wx.hideLoading();
                    if (data.data.data) {
                        let url = '/pages/index/index'
                        wx.reLaunch({
                            url
                        });
                    } else {
                        _this.setData({
                            showLoginBox: true,
                            isAuthor: true
                        })
                    }
                }
            })
        }
    },
    onShow() {
        let _this = this;
        wx.getSetting({
            success: settingData => {
                let authorFlag = settingData.authSetting['scope.userInfo']
                if (authorFlag) {
                    _this.checkAuthor(authorFlag);
                } else {
                    _this.setData({
                        showLoginBox: false,
                        isAuthor: false
                    })
                }
            }
        })

        let pages = getCurrentPages();
        let curPage = pages[pages.length - 1];
        app.checkBind(curPage.route);
    }
})